/*
* Copyright (C) 2012 Manuel Pérez
*
* This file is part of OpenSudoku.
*
* OpenSudoku is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* OpenSudoku is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with OpenSudoku. If not, see <http://www.gnu.org/licenses/>.
*
*/
package igraf.fakturak;

import igraf.ItzuliInterfazea;
import igraf.Panela;
import igraf.fakturak.javapdf.javapdf;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.SystemColor;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SwingConstants;

import dBasea.AbokatuFakt;
import dBasea.BezeroFaktura;
import dBasea.Bezeroa;
import dBasea.Inplikatuak;

public class KonponketaFaktura extends Panela{
	private static final long serialVersionUID = -9219466661684890160L;
	
	private JLabel zenbaki, zenbakia, dirua;
	private JTextField zenb, zenba, diru;
	private JButton ok, ezeztatu;
	private AbokatuFakt abok=new AbokatuFakt();
	private JButton aurkitu;
	private Inplikatuak inpli=new Inplikatuak();
	private JTextArea arra;
	private Bezeroa beze=new Bezeroa();
	private JButton bilatu;
	private BezeroFaktura bf=new BezeroFaktura();
	
	public KonponketaFaktura(String tituloa, final ItzuliInterfazea itzuliInterfazea) {		
		super(tituloa);

		JPanel erdikoPanela = this.getErdikoPanela();
		
		erdikoPanela.setBackground(SystemColor.activeCaption);
		erdikoPanela.setLayout(new BorderLayout());
		
		//Erdiko panelak
		JPanel erdikoa = new JPanel();
		erdikoa.setBackground(SystemColor.activeCaption);		
		erdikoPanela.add(erdikoa, BorderLayout.CENTER);
		erdikoa.setLayout(new GridLayout(9,1));
		
		JPanel ge=new JPanel();
		ge.setBackground(SystemColor.activeCaption);
		erdikoa.add(ge);
		JPanel g=new JPanel();
		g.setBackground(SystemColor.activeCaption);
		erdikoa.add(g);
		
		JPanel arri = new JPanel();
		arri.setBackground(SystemColor.activeCaption);
		FlowLayout fl_arri = new FlowLayout();
		fl_arri.setHgap(15);
		fl_arri.setVgap(10);
		arri.setLayout(fl_arri);
		zenbaki=new JLabel("Istripua");
		zenbaki.setFont(new Font("Arial", Font.PLAIN, 14));
		zenb=new JTextField(10);
		aurkitu = new JButton("Bilatu");
		aurkitu.setIcon(new ImageIcon(KonponketaFaktura.class.getResource("/Argazkia/lupa-32.png")));
		arri.add(zenbaki);
		arri.add(zenb);
		arri.add(aurkitu);
		erdikoa.add(arri);
		
		JPanel arrib = new JPanel();
		arrib.setBackground(SystemColor.activeCaption);
		FlowLayout fl_carri = new FlowLayout();
		fl_carri.setHgap(15);
		fl_carri.setVgap(10);
		arrib.setLayout(fl_carri);
		zenbakia=new JLabel("Bezeroaren NAN-a");
		zenbakia.setFont(new Font("Arial", Font.PLAIN, 14));
		zenba=new JTextField(15);
		arrib.add(zenbakia);
		arrib.add(zenba);
		erdikoa.add(arrib);
		
		JPanel xx = new JPanel();
		xx.setBackground(SystemColor.activeCaption);
		FlowLayout fl = new FlowLayout();
		fl.setHgap(15);
		fl.setVgap(10);
		xx.setLayout(fl);
		dirua=new JLabel("Prezioa:");
		dirua.setFont(new Font("Arial", Font.PLAIN, 14));
		diru=new JTextField(8);
		xx.add(dirua);
		xx.add(diru);
		erdikoa.add(xx);
		
		JPanel eh=new JPanel();
		eh.setBackground(SystemColor.activeCaption);
		erdikoa.add(eh);
		
		JLabel lblArrazoiak = new JLabel("Arrazoiak:");
		eh.add(lblArrazoiak);
		lblArrazoiak.setFont(new Font("Arial", Font.PLAIN, 14));
		
		JPanel e=new JPanel();
		e.setBackground(SystemColor.activeCaption);
		erdikoa.add(e);
		arra=new JTextArea();
		arra.setColumns(40);
		arra.setRows(6);
		e.add(arra);
		
		JPanel c=new JPanel();
		c.setBackground(SystemColor.activeCaption);
		erdikoa.add(c);
		
		//beheko panela
		JPanel behekoa = new JPanel();
		FlowLayout flowLayout = (FlowLayout) behekoa.getLayout();
		flowLayout.setVgap(15);
		flowLayout.setHgap(110);
		behekoa.setBackground(SystemColor.activeCaption);
		ok = new JButton("Faktura sortu");
		ok.setHorizontalAlignment(SwingConstants.LEFT);
		ok.setFont(new Font("Arial", Font.PLAIN, 14));
		behekoa.add(ok);
		ezeztatu = new JButton("Ezeztatu");
		ezeztatu.setHorizontalAlignment(SwingConstants.RIGHT);
		ezeztatu.setFont(new Font("Arial", Font.PLAIN, 14));
		behekoa.add(ezeztatu);
		erdikoPanela.add(behekoa, BorderLayout.SOUTH);
		
		
		bilatu = new JButton("Bilatu");
		bilatu.setIcon(new ImageIcon(KonponketaFaktura.class.getResource("/Argazkia/lupa-32.png")));
		
		arrib.add(bilatu);
		zenba.setEnabled(false);
		ezeztatu.addActionListener(new ActionListener() {			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				itzuliInterfazea.itzuliLehenPanela();
				zenb.setEnabled(true);
				zenba.setEnabled(false);
			}
		});
		aurkitu.addActionListener(new ActionListener() {			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				String zer = zenb.getText();				
				if (zer.isEmpty()) {
					JOptionPane.showMessageDialog(null, "Sartu istripu bat zuriunean, mesedez!", "Oker", JOptionPane.ERROR_MESSAGE);
				}
				else {
					try {
						int istripua = Integer.parseInt(zer);
						if (abok.badagoIstripua(istripua)) {
								zenb.setEnabled(false);
								zenba.setEnabled(true);

							}			
						else {
							JOptionPane.showMessageDialog(null, "Ez da existitzen sartu duzuna", "Oker", JOptionPane.ERROR_MESSAGE);
						}
					}
					catch (SQLException e) {					
						e.printStackTrace();
					}
				}
			}

		});	
		bilatu.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				String zer = zenba.getText();
				if (zer.isEmpty()) {
					JOptionPane.showMessageDialog(null,
							"Sartu bezero bat zuriunean, mesedez!", "Oker",
							JOptionPane.ERROR_MESSAGE);
				} else {
					try {
						
						if (beze.existitzenDa(zer)
								&& (inpli.inplikatuaDa(zer,
										Integer.parseInt(zenb.getText()))))
							
						{

							zenba.setEnabled(false);
							ezarri(false);

						}
						else {
							JOptionPane.showMessageDialog(null, "Ez da existitzen sartu duzuna", "Oker", JOptionPane.ERROR_MESSAGE);
						}

					} catch (SQLException e) {
						JOptionPane.showMessageDialog(null,
								"Ezin izan da aurkitu", "Oker",
								JOptionPane.ERROR_MESSAGE);
						e.printStackTrace();
					}
				}

			}
		});
		ok.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				try {
					if (!zerbaitIdatzi()) {
						JOptionPane.showMessageDialog(null,
								"Sartu behar duzu zerbait zuriunetan\n"
										+ "Ezin da eremu hutsik egon", "Oker",
								JOptionPane.ERROR_MESSAGE);
					} else {
						try {
							int zenbatekoa = Integer.parseInt(diru.getText());
							bf.bezFaktSortu(zenb.getText(), zenba.getText(), zenbatekoa, arra.getText());
							JOptionPane.showMessageDialog(null,
									"Ondo sortu da faktura", "",
									JOptionPane.INFORMATION_MESSAGE);
							
							javapdf pdf = new javapdf();
							
							pdf.crear_PDF(
									"Bezero: " + zenba.getText() + " ",
									"OpenSecur",
									"",
									"",
									"Egun on adiskide: \n\n "
											+ "Gure aseguru-etxea empresa komunikatzen dizu harro dagoela gure enpresa aukeratu duzulako. Hurrengoa ´"
											+ zenb.getText()
											+ "´ istripuaren fakturazioa da. Bertan, zuk "
											+ zenba.getText()
											+ " NAN-arekin, parte hartu zenuen istripu horretan. Eskertzen dizugu zure konfidantza.Bertan ´"
											+ arra.getText()
											+ "´ izan dira parte aktan jaso duguna eta beraz,asegurua gainontzekikoa izanik, ordaindu behar dituzu konponketaren kosteak." +
													"Hurrengo egunetan "
											+ diru.getText()
											+ " €-ko faktura jasoko dituzu zure helbidean. \n\n "
											+ "Agur bero bat: \n\n\n"
											+ " Enpresako zuzendaritza");

							eremuakGarbitu();
							zenba.setEnabled(false);
						} catch (NumberFormatException nfe) {
							JOptionPane.showMessageDialog(null,
									"Zenbakiak sartu zenbatekoa eremuan",
									"Oker", JOptionPane.ERROR_MESSAGE);
						}
					}
				} catch (Exception e) {
					JOptionPane.showMessageDialog(null,
							" Errepikatuta dago faktura hau.\n " +
							"Begiratu ea eremuren bat ez dagoen errepikaturik ",
							"Oker", JOptionPane.ERROR_MESSAGE);
					e.printStackTrace();
				}
			}		
		});
	}

	private boolean zerbaitIdatzi() {
		boolean aurki = false;

		aurki = zenb.getText().length() > 0;
		return aurki;
	}

	private void ezarri(boolean balioa) {
		zenb.setEnabled(balioa);		
		diru.setEnabled(!balioa);
		ok.setEnabled(!balioa);
	}

	@Override
	public void eremuakGarbitu() {
		zenb.setText("");
		zenba.setText("");
		diru.setText("");
		arra.setText("");
		ezarri(true);
	}

}
